ext.define('extension.slider', function() {

var animation = extension.animation;

function easing(timediff, base, change, duration) {
    var v = (timediff === duration) ? base + change : change *
        (-Math.pow(2, -10 * timediff / duration) + 1) + base;
    return v;
}

//     bounds(x/z) based on width/height properties.
//     100% --- 0%
//      |
//      |
//      0%
function descriptor(e) {
    var o = {};
    if (e.target.classList.contains('vertical-split')) {
        var length = 0 | e.target.offsetWidth;
        o.bbox = e.target.parentNode.offsetWidth;
        o.mouse = length + e.target.offsetLeft - e.clientX;
        o.axis = 'clientX';
        o.prop = 'right';
        o.s = 'previousElementSibling';
        o.c = o.bbox;
    }
    if (e.target.classList.contains('horizontal-split')) {
        var length = 0 | e.target.offsetHeight;
        o.bbox = e.target.parentNode.offsetHeight;
        o.mouse = length + e.target.offsetTop - e.clientY;
        o.axis = 'clientY';
        o.prop = 'bottom';
        o.s = 'nextElementSibling';
        o.c = 0;
    }
    o.x = 0 | (e.target.dataset.min * o.bbox / 100);
    o.z = 0 | (e.target.dataset.max * o.bbox / 100);
    return o;
}

function slide(e, p, c) {
    var u = {};
    u['flex-basis'] = {value: p, unit: 'px'};
    animation.start(e, u, {
        duration: 300,
        callback: c,
        easing: easing
    });
}

function collapseNextSibling(e, c) {
    e.dataset.pos = e.parentNode.offsetHeight - (e.offsetTop + e.offsetHeight);
    slide(e.nextElementSibling, 0, c);
}

function expandNextSibling(e, c) {
    var d = descriptor({target: e, clientY: 0});
    var p = Math.min(d.z, Math.max(d.x, e.dataset.pos));
    slide(e.nextElementSibling, p, c);
}

function createSlider(e) {
    var n = e.target;
    var d = descriptor(e);
    var s = n[d.s].style;
    return function(e) {
        var p = Math.max(d.x, Math.min(d.z, d.bbox - e[d.axis] - d.mouse));
        s['flex-basis'] = (d.c > 0 ? d.c - p : p) + 'px';
    }
}

return {
    create: createSlider,
    expand: expandNextSibling,
    collapse: collapseNextSibling
};

});
